/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Events;
using Engine;

namespace Core.Model.Ingredients.Presentable
{
    public class Stream:Presentable
    {

        #region Exchnaged Attributes

        private List<Ingredient> multiplex;

        public List<Ingredient> Multiplex
        {
            get { return multiplex; }
            set { multiplex = value; }
        }

        private Boolean storage;

        public Boolean Storage
        {
            get { return storage; }
            set { storage = value; }
        }

        private int looping = 1;

        public int Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        #endregion

        #region Internal Behaviours

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();

            Engine.MHEGEngine.Instance.ApplicationStream = this;
        }


        public override void Prepare ()
        {
            if ( AvailabilityStatus )
                return;

            InittInternalAttributes();

            foreach ( Ingredient component in Multiplex )
            {
                if ( component.InitiallyActive )
                {
                    component.Activate();

                }

            }

            AvailabilityStatus = true;

            //  Preparation behaviour complete
            Engine.MHEGEngine.Instance.Handle( new IsAvailable( this ) );

            //Async
            Engine.MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );
        }
        
        public override void Activate ()
        {
            if ( !RunningStatus)
            {
                base.Activate();

                //start playing all active stream components

                RunningStatus = true;
                Engine.MHEGEngine.Instance.Handle( new IsRunning( this ) );
            }
        }
                
        public override void Deactivate ()
        {
            if ( RunningStatus )
            {

                //stop playing all active stream components
                base.Deactivate();
            }
        }

        public override void Destroy ()
        {
            if ( AvailabilityStatus )
            {
                List<Ingredient> Reverse = new List<Ingredient>( Multiplex );
                Reverse.Reverse();

                foreach ( Ingredient component in Reverse )
                {
                    component.Destroy();
                }

                base.Destroy();
            }
        }

        public override void SetData (Core.Model.Data.Generics.GenericContentRef newContent )
        {
            base.SetData( newContent );
            Engine.MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );

        }

        public override void SetData (Core.Model.Data.Generics.GenericOctetString newContent )
        {
            base.SetData( newContent );
            Engine.MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );
        }
        #endregion
    }
}
